<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 优化 REST API 性能的深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            position: relative;
        }
        .code-block:before {
            content: '';
            position: absolute;
            top: 12px;
            left: 12px;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #ff5f56;
            box-shadow: 20px 0 #ffbd2e, 40px 0 #27c93f;
        }
        .hover-scale {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .hover-scale:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .mermaid {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 md:py-32 px-4">
        <div class="max-w-5xl mx-auto text-center">
            <div class="inline-block px-4 py-1 mb-4 bg-white bg-opacity-20 rounded-full">
                <span class="text-sm font-medium">性能优化指南</span>
            </div>
            <h1 class="text-4xl md:text-6xl font-bold mb-6">Spring Boot 优化 REST API 性能</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto mb-8">深度解析从架构设计到代码实现的全面性能优化策略</p>
            <div class="flex justify-center space-x-4">
                <a href="#optimization-strategies" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-lg hover:bg-opacity-90 transition">探索优化策略</a>
                <a href="#mermaid-chart" class="px-6 py-3 border border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition">查看关系图</a>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-5xl mx-auto px-4 py-12 md:py-20">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">为什么需要优化 REST API 性能？</h2>
                <p class="text-gray-600 max-w-3xl mx-auto">在现代Web应用中，API性能直接影响用户体验和系统扩展性。优化目标是减少响应时间、提升吞吐量以及降低服务器资源占用。</p>
            </div>
            
            <div class="grid md:grid-cols-3 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-sm hover-scale">
                    <div class="text-indigo-500 mb-4 text-3xl"><i class="fas fa-tachometer-alt"></i></div>
                    <h3 class="text-xl font-semibold mb-2">响应时间</h3>
                    <p class="text-gray-600">更快的API响应意味着更好的用户体验和更高的用户留存率。</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm hover-scale">
                    <div class="text-indigo-500 mb-4 text-3xl"><i class="fas fa-server"></i></div>
                    <h3 class="text-xl font-semibold mb-2">资源效率</h3>
                    <p class="text-gray-600">优化后的API可以处理更多请求，同时消耗更少的服务器资源。</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm hover-scale">
                    <div class="text-indigo-500 mb-4 text-3xl"><i class="fas fa-chart-line"></i></div>
                    <h3 class="text-xl font-semibold mb-2">可扩展性</h3>
                    <p class="text-gray-600">良好的性能基础使系统更容易应对流量增长和业务扩展。</p>
                </div>
            </div>
        </section>

        <!-- Optimization Strategies -->
        <section id="optimization-strategies" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-center">六大优化策略</h2>
            
            <!-- 1. Architecture Optimization -->
            <div class="bg-white rounded-xl shadow-sm overflow-hidden mb-12 hover-scale">
                <div class="p-6 border-b border-gray-100 bg-gray-50">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">1</span>
                        架构层优化
                    </h3>
                </div>
                <div class="p-6">
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-exchange-alt text-indigo-500 mr-2"></i>
                            启用 HTTP/2
                        </h4>
                        <p class="text-gray-600 mb-4">HTTP/2 支持多路复用、头部压缩和服务器推送，可以显著提升 API 的响应效率。</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto mb-4">
                            <pre><code class="language-properties">server.http2.enabled=true</code></pre>
                        </div>
                    </div>
                    
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-balance-scale text-indigo-500 mr-2"></i>
                            使用负载均衡与集群
                        </h4>
                        <p class="text-gray-600 mb-4">单节点服务器性能有限，通过负载均衡分发请求，提升整体性能。</p>
                    </div>
                    
                    <div>
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-sync-alt text-indigo-500 mr-2"></i>
                            异步非阻塞处理
                        </h4>
                        <p class="text-gray-600 mb-4">默认 REST API 是同步阻塞的，在高并发场景可能导致线程资源耗尽。通过异步方式，可以释放线程资源。</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto">
                            <pre><code class="language-java">@RestController
public class AsyncController {
    @GetMapping("/async")
    public CompletableFuture&lt;String&gt; asyncEndpoint() {
        return CompletableFuture.supplyAsync(() -&gt; "Hello, Async!");
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 2. Data Layer Optimization -->
            <div class="bg-white rounded-xl shadow-sm overflow-hidden mb-12 hover-scale">
                <div class="p-6 border-b border-gray-100 bg-gray-50">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">2</span>
                        数据层优化
                    </h3>
                </div>
                <div class="p-6">
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-database text-indigo-500 mr-2"></i>
                            减少数据库查询次数
                        </h4>
                        <p class="text-gray-600 mb-4">频繁的数据库查询会导致响应延迟。优化方式包括批量查询、缓存机制和避免 N+1 问题。</p>
                    </div>
                    
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-plug text-indigo-500 mr-2"></i>
                            数据库连接池优化
                        </h4>
                        <p class="text-gray-600 mb-4">数据库连接是 REST API 的性能瓶颈之一。调整连接池配置：</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto">
                            <pre><code class="language-properties">spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.connection-timeout=30000</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-layer-group text-indigo-500 mr-2"></i>
                            分页与延迟加载
                        </h4>
                        <p class="text-gray-600 mb-4">直接加载所有数据会占用大量内存，延迟加载和分页可以优化查询效率。</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto">
                            <pre><code class="language-java">@GetMapping("/users")
public Page&lt;User&gt; getUsers(Pageable pageable) {
    return userService.findAll(pageable);
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 3. Network & Communication -->
            <div class="bg-white rounded-xl shadow-sm overflow-hidden mb-12 hover-scale">
                <div class="p-6 border-b border-gray-100 bg-gray-50">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">3</span>
                        网络与通信层优化
                    </h3>
                </div>
                <div class="p-6">
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-compress-arrows-alt text-indigo-500 mr-2"></i>
                            Gzip 压缩
                        </h4>
                        <p class="text-gray-600 mb-4">通过压缩 HTTP 响应体可以显著减少网络传输量。</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto">
                            <pre><code class="language-properties">server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html
server.compression.min-response-size=1024</code></pre>
                        </div>
                    </div>
                    
                    <div class="mb-6">
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-memory text-indigo-500 mr-2"></i>
                            缓存 HTTP 响应
                        </h4>
                        <p class="text-gray-600 mb-4">对于不频繁变动的数据，使用缓存减少服务器负载。</p>
                        <div class="code-block p-4 pl-16 overflow-x-auto">
                            <pre><code class="language-java">@GetMapping("/products")
@Cacheable("products")
public List&lt;Product&gt; getProducts() {
    return productService.findAll();
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="text-xl font-medium mb-3 flex items-center">
                            <i class="fas fa-file-code text-indigo-500 mr-2"></i>
                            减少 JSON 数据大小
                        </h4>
                        <p class="text-gray-600">过多的嵌套结构或无用字段会增加响应时间。使用 @JsonIgnore 或 DTO 只返回必要的数据。</p>
                    </div>
                </div>
            </div>
            
            <!-- Mermaid Diagram -->
            <div id="mermaid-chart" class="bg-white rounded-xl shadow-sm overflow-hidden mb-12 p-6 hover-scale">
                <h3 class="text-2xl font-semibold mb-6 text-center">优化策略关系图</h3>
                <div class="mermaid">
                    graph TD
                        A[Spring Boot API优化] --> B[架构层优化]
                        A --> C[数据层优化]
                        A --> D[网络与通信优化]
                        A --> E[应用层优化]
                        A --> F[Spring Boot特性]
                        A --> G[高级策略]
                        
                        B --> B1[HTTP/2]
                        B --> B2[负载均衡]
                        B --> B3[异步处理]
                        
                        C --> C1[减少查询]
                        C --> C2[连接池]
                        C --> C3[分页]
                        
                        D --> D1[Gzip]
                        D --> D2[缓存]
                        D --> D3[JSON优化]
                        
                        E --> E1[对象池]
                        E --> E2[日志优化]
                        E --> E3[线程池]
                        
                        F --> F1[Actuator]
                        F --> F2[超时配置]
                        
                        G --> G1[GraphQL]
                        G --> G2[CDN]
                        G --> G3[限流熔断]
                </div>
            </div>
            
            <!-- Additional Sections (4-6) -->
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 4. Application Layer -->
                <div class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                    <div class="p-6 border-b border-gray-100 bg-gray-50">
                        <h3 class="text-2xl font-semibold flex items-center">
                            <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">4</span>
                            应用层优化
                        </h3>
                    </div>
                    <div class="p-6">
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <span class="flex-shrink-0 w-6 h-6 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center mr-3 mt-1">
                                    <i class="fas fa-cube text-xs"></i>
                                </span>
                                <div>
                                    <h4 class="font-medium">使用对象池</h4>
                                    <p class="text-gray-600 text-sm">频繁创建和销毁对象会增加 GC 压力。使用对象池管理资源对象。</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <span class="flex-shrink-0 w-6 h-6 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center mr-3 mt-1">
                                    <i class="fas fa-scroll text-xs"></i>
                                </span>
                                <div>
                                    <h4 class="font-medium">减少日志开销</h4>
                                    <p class="text-gray-600 text-sm">调整日志级别为 INFO 或 WARN，避免 DEBUG 级别日志。</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <span class="flex-shrink-0 w-6 h-6 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center mr-3 mt-1">
                                    <i class="fas fa-users-cog text-xs"></i>
                                </span>
                                <div>
                                    <h4 class="font-medium">配置线程池</h4>
                                    <p class="text-gray-600 text-sm">线程池大小直接影响高并发下的性能。</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <!-- 5. Spring Boot Features -->
                <div class="bg-white rounded-xl shadow-sm overflow-hidden hover-scale">
                    <div class="p-6 border-b border-gray-100 bg-gray-50">
                        <h3 class="text-2xl font-semibold flex items-center">
                            <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">5</span>
                            Spring Boot 特性优化
                        </h3>
                    </div>
                    <div class="p-6">
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <span class="flex-shrink-0 w-6 h-6 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center mr-3 mt-1">
                                    <i class="fas fa-chart-pie text-xs"></i>
                                </span>
                                <div>
                                    <h4 class="font-medium">启用 Actuator 监控</h4>
                                    <p class="text-gray-600 text-sm">通过监控关键指标发现性能瓶颈。</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <span class="flex-shrink-0 w-6 h-6 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center mr-3 mt-1">
                                    <i class="fas fa-hourglass-half text-xs"></i>
                                </span>
                                <div>
                                    <h4 class="font-medium">配置响应超时</h4>
                                    <p class="text-gray-600 text-sm">未配置超时可能导致长时间阻塞线程。</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <!-- 6. Advanced Strategies -->
                <div class="bg-white rounded-xl shadow-sm overflow-hidden md:col-span-2 hover-scale">
                    <div class="p-6 border-b border-gray-100 bg-gray-50">
                        <h3 class="text-2xl font-semibold flex items-center">
                            <span class="w-8 h-8 flex items-center justify-center bg-indigo-500 text-white rounded-full mr-3">6</span>
                            高级优化策略
                        </h3>
                    </div>
                    <div class="p-6">
                        <div class="grid md:grid-cols-3 gap-6">
                            <div class="border border-gray-100 rounded-lg p-4">
                                <h4 class="font-medium mb-2 text-indigo-600 flex items-center">
                                    <i class="fas fa-project-diagram mr-2"></i>
                                    GraphQL
                                </h4>
                                <p class="text-gray-600 text-sm">可以按需返回数据，避免不必要的数据传输，提升性能。</p>
                            </div>
                            <div class="border border-gray-100 rounded-lg p-4">
                                <h4 class="font-medium mb-2 text-indigo-600 flex items-center">
                                    <i class="fas fa-network-wired mr-2"></i>
                                    CDN
                                </h4>
                                <p class="text-gray-600 text-sm">将静态资源和部分缓存数据分发到 CDN，可以减少服务器负载。</p>
                            </div>
                            <div class="border border-gray-100 rounded-lg p-4">
                                <h4 class="font-medium mb-2 text-indigo-600 flex items-center">
                                    <i class="fas fa-shield-alt mr-2"></i>
                                    限流与熔断
                                </h4>
                                <p class="text-gray-600 text-sm">保护系统在高负载或异常情况下的稳定性。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="max-w-5xl mx-auto px-4 text-center">
            <div class="mb-6">
                <h3 class="text-xl font-medium text-white mb-2">技术小馆</h3>
                <p class="text-gray-400">持续探索技术前沿，分享开发经验</p>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="inline-block px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition">访问语雀主页</a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>